package com.eidos.blogs.openapi.service.seckill.impl;

import com.eidos.blogs.openapi.dto.secKill.GenerateRedEnvelopeDto;
import com.eidos.blogs.openapi.entity.seckill.RedeReceiveRecord;
import com.eidos.blogs.openapi.exception.SourceException;
import com.eidos.blogs.openapi.rest.QueryParams;
import com.eidos.blogs.openapi.mapper.seckill.RedeReceiveRecordMapper;
import com.eidos.blogs.openapi.service.seckill.IRedeReceiveRecordService;
import com.eidos.blogs.openapi.thread.seckill.AsyncMapperExecutor;
import com.eidos.blogs.openapi.thread.seckill.TaskBackHandler;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ： Eidos
 * @date ：Created in 2021-01-12
 * @description：抢红包记录表 服务实现类
 * @modified By：
 * @version: 1.0
 */
@Service
public class RedeReceiveRecordServiceImpl extends ServiceImpl<RedeReceiveRecordMapper, RedeReceiveRecord> implements IRedeReceiveRecordService {

    @Autowired
    private RedeReceiveRecordMapper  redeReceiveRecordMapper;


    /**
     * 抢红包记录表分页列表
     *
     * @param param 根据需要进行传值
     * @return
     */
    @Override
    public IPage<RedeReceiveRecord> page(RedeReceiveRecord param) {
        Page page = QueryParams.getPage();
        QueryWrapper<RedeReceiveRecord> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                // 自增主键
                .eq(param.getId() != null, RedeReceiveRecord::getId, param.getId())
                // 红包ID
                .eq(param.getRedEnvelopeId() != null, RedeReceiveRecord::getRedEnvelopeId, param.getRedEnvelopeId())
                // 抢到红包用户的用户标识
                .eq(param.getUserId() != null, RedeReceiveRecord::getUserId, param.getUserId())
                // 抢到红包的金额
                .eq(param.getAmount() != null, RedeReceiveRecord::getAmount, param.getAmount())
                // 创建时间
                .eq(param.getCreateTime() != null, RedeReceiveRecord::getCreateTime, param.getCreateTime())
        ;
        return baseMapper.selectPage(page, wrapper);
    }

    /**
     * 抢红包记录表详情
     *
     * @param id
     * @return
     */
    @Override
    public RedeReceiveRecord info(Long id) {
        return getById(id);
    }

    /**
     * 抢红包记录表新增
     *
     * @param param 根据需要进行传值
     * @return
     */
    @Override
    public void add(RedeReceiveRecord param) {
        if (!save(param)) {
            throw new SourceException("新增失败");
        }
    }

    /**
     * 抢红包记录表修改
     *
     * @param param 根据需要进行传值
     * @return
     */
    @Override
    public void modify(RedeReceiveRecord param) {
        if (!updateById(param)) {
            throw new SourceException("保存失败");
        }
    }

    /**
     * 抢红包记录表删除(单个条目)
     *
     * @param id
     * @return
     */
    @Override
    public void remove(Long id) {
        removeById(id);
    }

    /**
     * 抢红包记录表删除(多个条目)
     *
     * @param ids
     * @return
     */
    @Override
    public void removes(List<Long> ids) {
        removeByIds(ids);
    }

    @Override
    public List<RedeReceiveRecord> queryRedeReceiveRecordListByRedEnvelopeId(GenerateRedEnvelopeDto dto, TaskBackHandler<List<RedeReceiveRecord>> callback) {

        try {
            Method method = redeReceiveRecordMapper.getClass().getDeclaredMethod("queryRedeReceiveRecordListByRedEnvelopeId",dto.getClass(),callback.getClass());
            AsyncMapperExecutor.executeRunnable(redeReceiveRecordMapper,method, dto,callback);
            return  null;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }

    }
}
